package com.rovinj_server.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

import com.rovinj_server.R;
import com.rovinj_server.entity.InventoryTask;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


public class FragmentTaskAdapter extends BaseAdapter {

    private Context mContext;

    private List<InventoryTask> mListDataBean = new ArrayList<>();
    private HashMap<Integer, Boolean> isSelected ;
    public int showCount = 20;

    public FragmentTaskAdapter(Context context, List<InventoryTask> mListDataBean) {
        this.mContext = context;
        this.mListDataBean = mListDataBean;
        isSelected = new HashMap<Integer, Boolean>();
        initDate();
    }

    // 初始化isSelected的数据
    private void initDate() {
        for (int i = 0; i < mListDataBean.size(); i++) {
            getIsSelected().put(i, false);
        }
    }

    public HashMap<Integer, Boolean> getIsSelected() {
        return isSelected;
    }

    public void setIsSelected(HashMap<Integer, Boolean> isSelected) {
        this.isSelected = isSelected;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
//		return mList.size();

        return showCount > mListDataBean.size() ? mListDataBean.size() : showCount;
    }

    @Override
    public Object getItem(int i) {
        // TODO Auto-generated method stub
        return mListDataBean.get(i);
    }

    @Override
    public long getItemId(int i) {
        // TODO Auto-generated method stub
        return mListDataBean.size();
    }

    @Override
    public View getView(int i, View convertView, ViewGroup viewgroup) {
        List<InventoryTask> list = new ArrayList<>();
        if (mListDataBean.size() > showCount) {
            list = mListDataBean.subList(0, showCount);
        } else {
            list = mListDataBean;
        }

        ViewHolder holder;
        if (convertView != null) {
            holder = (ViewHolder) convertView.getTag();
        } else {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.adapter_tasks, viewgroup, false);
            holder.code = (TextView) convertView.findViewById(R.id.code);
            holder.name = (TextView) convertView.findViewById(R.id.name);
            holder.crDate = (TextView) convertView.findViewById(R.id.crDate);
            holder.range = (TextView) convertView.findViewById(R.id.range);
            holder.sjQty = (TextView) convertView.findViewById(R.id.sj_qty);
            holder.range = (TextView) convertView.findViewById(R.id.range);
            holder.diffNumber = (TextView) convertView.findViewById(R.id.diffNumber);
            holder.cb = (CheckBox) convertView.findViewById(R.id.cb);
            convertView.setTag(holder);
            AutoUtils.autoSize(convertView);
        }

        if(mListDataBean!=null&&mListDataBean.size()>0){
         InventoryTask inventoryData = mListDataBean.get(i);
         holder.crDate.setText(inventoryData.getCrDate());
         holder.name.setText(inventoryData.getName());
         holder.code.setText(inventoryData.getCode());
         holder.range.setText(inventoryData.getRange());
         holder.sjQty.setText(inventoryData.getSpQty() + "");
         holder.diffNumber.setText(inventoryData.getDiffQty() + "");
         viewgroup.setBackgroundResource(R.drawable.usermainpage_bg_center);
          holder.cb.setChecked(getIsSelected().get(i));
        }
        return convertView;
    }

    public void setListDataBean(List<InventoryTask> listDataBean) {
        mListDataBean = listDataBean;
    }

    public static class ViewHolder {
        public TextView code;
        public TextView name;
        public TextView crDate;
        public TextView range;
        public TextView sjQty;
        public TextView diffNumber;
        public CheckBox cb;
    }

}
